package com.qfedu.service.impl;

import com.qfedu.common.api.CommonResult;
import com.qfedu.mbg.mapper.SmsStaffMapper;
import com.qfedu.mbg.model.SmsStaff;
import com.qfedu.mbg.model.SmsStaffExample;
import com.qfedu.service.SmsStaffService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class SmsStaffServiceImpl implements SmsStaffService {



    @Autowired
    private SmsStaffMapper smsStaffMapper;


    // 密码加密的注解
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public List<SmsStaff> selectAll() {

        SmsStaffExample smsStaffExample = new SmsStaffExample();

        return smsStaffMapper.selectByExample(smsStaffExample);

    }

    @Override
    public SmsStaff createUser(SmsStaff smsStaff) {

        SmsStaff staff = new SmsStaff();
        staff.setCreateTime(new Date());
        staff.setUsername(smsStaff.getUsername());
        staff.setPassword(smsStaff.getPassword());
        staff.setPhone(smsStaff.getPhone());
        staff.setDeptId(smsStaff.getDeptId());
        staff.setProId(smsStaff.getProId());
        staff.setRoleId(smsStaff.getRoleId());
        staff.setStatus(smsStaff.getStatus());

        // 对存进数据库的密码进行加密
        String encodePassword = passwordEncoder.encode(smsStaff.getPassword());
        staff.setPassword(encodePassword);
        smsStaffMapper.insert(staff);

        return staff;
    }

    @Override
    public SmsStaff updateUser(SmsStaff smsStaff) {

        SmsStaff staff = new SmsStaff();
        staff.setCreateTime(new Date());
        staff.setUsername(smsStaff.getUsername());
        staff.setPassword(smsStaff.getPassword());
        staff.setPhone(smsStaff.getPhone());
        staff.setDeptId(smsStaff.getDeptId());
        staff.setProId(smsStaff.getProId());
        staff.setRoleId(smsStaff.getRoleId());
        staff.setStatus(smsStaff.getStatus());
        staff.setId(smsStaff.getId());

        String encodePassword = passwordEncoder.encode(smsStaff.getPassword());
        staff.setPassword(encodePassword);
        smsStaffMapper.updateByPrimaryKeySelective(staff);
        return staff;
    }

    @Override
    public int delete(List<Long>ids) {
        SmsStaffExample smsStaffExample = new SmsStaffExample();

        SmsStaffExample.Criteria criteria = smsStaffExample.createCriteria();
        criteria.andIdIn(ids);
        return   smsStaffMapper.deleteByExample(smsStaffExample);
    }


}
